#region Namespace References

using System.IO;
using System.Xml;
using DynConDevCorp.OpenArch.SmartData.BillingDemo1.ApplicationBusinessObjects;
using DynConDevCorp.OpenArch.SmartData.Core;
using DynConDevCorp.OpenArch.SmartData.Interfaces;
using DynConDevCorp.OpenArch.SmartData.Persistance;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.BillingDemo1.BusinessLogic
{
    internal class MasterDataStore
    {
        #region Constructors

        private MasterDataStore()
        {
            LoadData();
        }

        #endregion

        #region Public Static Properties

        public static MasterDataStore GlobalInstance
        {
            get { return s_GlobalInstance; }
        }

        #endregion

        #region Public Instance Methods

        public Customer GetCustomer()
        {
            foreach (Customer customer in Customers)
                return customer;
            return new Customer();
        }

        public Order GetOrder()
        {
            foreach (Order order in Orders)
                return order;
            return new Order();
        }

        #endregion

        private void LoadData()
        {
            SmartEntityInstanceGraph objectGraph;
            using (TextReader textReader = new StreamReader("SmartNorthWind.xml"))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreWhitespace = true;
                using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
                {
                    SmartGraphReaderXml graphReader = new SmartGraphReaderXml(xmlReader);
                    objectGraph = graphReader.ReadAll();
                }
            }
            m_Customers = objectGraph.GetCollection<Customer>("customers");
            m_Products = objectGraph.GetCollection<Product>("products");
            m_Orders = objectGraph.GetCollection<Order>("orders");
            m_OrderDetails = objectGraph.GetCollection<OrderDetail>("orderDetails");
        }

        public ISmartEntityCollection<Customer> Customers { get { return m_Customers; } }
        public ISmartEntityCollection<Product> Products { get { return m_Products; } }
        public ISmartEntityCollection<Order> Orders { get { return m_Orders; } }
        public ISmartEntityCollection<OrderDetail> OrderDetails { get { return m_OrderDetails; } }

        private ISmartEntityCollection<Customer> m_Customers;
        private ISmartEntityCollection<Product> m_Products;
        private ISmartEntityCollection<Order> m_Orders;
        private ISmartEntityCollection<OrderDetail> m_OrderDetails;

        #region Private Static Methods

        private static Customer CreateDefaultCustomer()
        {
#if true
            return new Customer();
#else
            Customer retVal = new Customer
                                  {
                                      CustomerCode = {Value = "1000"},
                                      CompanyName = {Value = "SampleCustomer"}
                                  };
            retVal.ContactTelephone.AreaCode.Value = "800";
            retVal.ContactTelephone.Exchange.Value = "555";
            retVal.ContactTelephone.Number.Value = "1212";

            retVal.BillingAddress.StreetAddress.Value = "123 Main Street";
            retVal.BillingAddress.StreetAuxAddress.Value = "Suite 2525";
            retVal.BillingAddress.City.Value = "Anytown";
            retVal.BillingAddress.State.Value = "NY";
            retVal.BillingAddress.ZipCode.Value = "10019";

            retVal.ShippingAddress.StreetAddress.Value = "123 Main Street";
            retVal.ShippingAddress.StreetAuxAddress.Value = "Suite 2525";
            retVal.ShippingAddress.City.Value = "Anytown";
            retVal.ShippingAddress.State.Value = "NY";
            retVal.ShippingAddress.ZipCode.Value = "10019";

            return retVal;
#endif
        }

        private static Order CreateDefaultOrder()
        {
#if true
            return new Order();
#else
            Order retVal = new Order
                                 {
                                     OrderNumber = {Value = "1000"},
                                     OrderDate = {Value = DateTime.Today},
                                     TaxRate = {Value = 0.07M},
                                     ShippingRate = {Value = 1.23M}
                                 };


            retVal.AddItem(new OrderDetail
                               {
                                   Quantity = {Value = 10},
                                   UnitPrice = {Value = 19.95M},
                                   Weight = {Value = 0.75M}
                               });


            retVal.AddItem(new OrderDetail
                               {
                                   Quantity = {Value = 5},
                                   UnitPrice = {Value = 59.95M},
                                   Weight = {Value = 0.75M}
                               });


            retVal.AddItem(new OrderDetail
                               {
                                   Quantity = {Value = 100},
                                   UnitPrice = {Value = 0.95M},
                                   Weight = {Value = 0.75M}
                               });

            return retVal;
#endif
        }

        #endregion

        #region Fields and Constants

        private static readonly MasterDataStore s_GlobalInstance = new MasterDataStore();

        private Customer m_DefaultCustomer;
        private Order m_DefaultOrder;

        #endregion
    }
}